home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / vroom / messages.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  6.2 KB  |  233 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <strings.h>
  20.  
  21. #include <X11/Intrinsic.h>
  22. #include <Xm/Xm.h>
  23. #include <Xm/Form.h>
  24. #include <Xm/Label.h>
  25. #include <Xm/Text.h>
  26. #include <Xm/TextF.h>
  27. #include <Xm/Separator.h>
  28.  
  29. #include "vroom.h"
  30. #include "client.h"
  31. #include "car.h"
  32.  
  33. /* BEGIN PROTOTYPES -S messages.c */
  34. static void     sendMessage( Widget w, XtPointer clientData,
  35.                     XtPointer callData ) ;
  36. /* END PROTOTYPES -S messages.c */
  37.  
  38. #define    VISIBLE_LINES    (5)
  39. #define    LINE_LENGTH    (VROOM_NAMELEN+3+VROOM_MSGLEN)
  40.  
  41. extern Widget    topLevel ;
  42. extern Widget    introWindow ;
  43. extern Widget    messageForm ;
  44. extern Car    cars[] ;
  45.  
  46. static Widget    textArea = NULL ;
  47. static char    line[VISIBLE_LINES][LINE_LENGTH] ;
  48. static char    nextLine = 0 ;
  49. static char    numLines = 0 ;
  50.  
  51.  
  52.  
  53. /*------------------------------------------------------------------------------
  54.  * Create the message area.
  55.  *----------------------------------------------------------------------------*/
  56. void
  57. createMessageArea(
  58.     void
  59.     )
  60. {
  61.     int        n ;
  62.     Arg        args[10] ;
  63.     XmString    str ;
  64.     Widget        separator ;
  65.     Widget        label ;
  66.     Widget        tf ;
  67.  
  68.     if( textArea != NULL )
  69.     {
  70.         return ;
  71.     }
  72.  
  73.     n = 0 ;
  74.     XtSetArg( args[n], XmNorientation, XmHORIZONTAL ) ; n++ ;
  75.     XtSetArg( args[n], XmNtopAttachment, XmATTACH_FORM ) ; n++ ;
  76.     XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ) ; n++ ;
  77.     XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ) ; n++ ;
  78.     XtSetArg( args[n], XmNbottomAttachment, XmATTACH_NONE ) ; n++ ;
  79.     separator = XtCreateManagedWidget( "separator", xmSeparatorWidgetClass,
  80.                     messageForm, args, n ) ;
  81.  
  82.     n = 0 ;
  83.     str = XmStringCreateLocalized( "Incoming messages:" ) ;
  84.     XtSetArg( args[n], XmNtopAttachment, XmATTACH_WIDGET ) ; n++ ;
  85.     XtSetArg( args[n], XmNtopWidget, separator ) ; n++ ;
  86.     XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ) ; n++ ;
  87.     XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ) ; n++ ;
  88.     XtSetArg( args[n], XmNbottomAttachment, XmATTACH_NONE ) ; n++ ;
  89.     XtSetArg( args[n], XmNlabelString, str ) ; n++ ;
  90.     XtSetArg( args[n], XmNalignment, XmALIGNMENT_BEGINNING ) ; n++ ;
  91.     label = XtCreateManagedWidget( "label", xmLabelWidgetClass,
  92.                     messageForm, args, n ) ;
  93.     XmStringFree( str ) ;
  94.  
  95.     n = 0 ;
  96.     XtSetArg( args[n], XmNrows, VISIBLE_LINES ) ; n++ ;
  97.     XtSetArg( args[n], XmNeditMode, XmMULTI_LINE_EDIT ) ; n++ ;
  98.     XtSetArg( args[n], XmNeditable, False ) ; n++ ;
  99.     XtSetArg( args[n], XmNtopAttachment, XmATTACH_WIDGET ) ; n++ ;
  100.     XtSetArg( args[n], XmNtopWidget, label ) ; n++ ;
  101.     XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ) ; n++ ;
  102.     XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ) ; n++ ;
  103.     XtSetArg( args[n], XmNbottomAttachment, XmATTACH_NONE ) ; n++ ;
  104.     textArea = XtCreateManagedWidget( "incomingText", xmTextWidgetClass,
  105.                         messageForm, args, n ) ;
  106.  
  107.     XmTextSetString( textArea, "" ) ;
  108.  
  109.     n = 0 ;
  110.     str = XmStringCreateLocalized( "Outgoing message:" ) ;
  111.     XtSetArg( args[n], XmNtopAttachment, XmATTACH_WIDGET ) ; n++ ;
  112.     XtSetArg( args[n], XmNtopWidget, textArea ) ; n++ ;
  113.     XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ) ; n++ ;
  114.     XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ) ; n++ ;
  115.     XtSetArg( args[n], XmNbottomAttachment, XmATTACH_NONE ) ; n++ ;
  116.     XtSetArg( args[n], XmNlabelString, str ) ; n++ ;
  117.     XtSetArg( args[n], XmNalignment, XmALIGNMENT_BEGINNING ) ; n++ ;
  118.     label = XtCreateManagedWidget( "label", xmLabelWidgetClass,
  119.                         messageForm, args, n ) ;
  120.     XmStringFree( str ) ;
  121.  
  122.     n = 0 ;
  123.     XtSetArg( args[n], XmNtopAttachment, XmATTACH_WIDGET ) ; n++ ;
  124.     XtSetArg( args[n], XmNtopWidget, label ) ; n++ ;
  125.     XtSetArg( args[n], XmNleftAttachment, XmATTACH_FORM ) ; n++ ;
  126.     XtSetArg( args[n], XmNrightAttachment, XmATTACH_FORM ) ; n++ ;
  127.     XtSetArg( args[n], XmNbottomAttachment, XmATTACH_FORM ) ; n++ ;
  128.     XtSetArg( args[n], XmNmaxLength, VROOM_MSGLEN - 1 ) ; n++ ;
  129.     tf = XtCreateManagedWidget( "outgoingText", xmTextFieldWidgetClass,
  130.                         messageForm, args, n ) ;
  131.  
  132.     XtAddCallback( tf, XmNactivateCallback, sendMessage, NULL ) ;
  133. }
  134.  
  135.  
  136.  
  137. /*------------------------------------------------------------------------------
  138.  * Send a message to the server.
  139.  *----------------------------------------------------------------------------*/
  140. static void
  141. sendMessage(
  142.     Widget        w,
  143.     XtPointer    clientData,
  144.     XtPointer    callData
  145.     )
  146. {
  147.     char    *msg ;
  148.     char    *p ;
  149.  
  150.     msg = XmTextFieldGetString( w ) ;
  151.  
  152.     if( ( p = strchr( msg, '\n' ) ) != NULL )
  153.     {
  154.         *p = '\0' ;
  155.     }
  156.  
  157.     if( strlen( msg ) )
  158.     {
  159.         sendServerMsgPacket( msg ) ;
  160.     }
  161.  
  162.     XtFree( msg ) ;
  163.  
  164.     XmTextFieldSetString( w, "" ) ;
  165. }
  166.  
  167.  
  168.  
  169. /*------------------------------------------------------------------------------
  170.  * Post a new message to the screen.
  171.  *----------------------------------------------------------------------------*/
  172. void
  173. postNewMessage(
  174.     int    nPlayer,
  175.     char    *msg
  176.     )
  177. {
  178.     int    i ;
  179.     int    l ;
  180.     int    n ;
  181.     int    k ;
  182.     char    text[VISIBLE_LINES*(LINE_LENGTH+1)+1] ;
  183.  
  184.     if( nPlayer < 0 )
  185.     {
  186.         l = 0 ;
  187.     }
  188.     else
  189.     {
  190.         l = strlen( cars[nPlayer].name ) ;
  191.         strncpy( line[nextLine], cars[nPlayer].name, l ) ;
  192.         line[nextLine][l++] = ':' ;
  193.         line[nextLine][l++] = ' ' ;
  194.     }
  195.     k = 0 ;
  196.     while( k < VROOM_MSGLEN - 1 && msg[k] != '\0' && msg[k] != '\n' )
  197.     {
  198.         line[nextLine][l++] = msg[k++] ;
  199.     }
  200.     line[nextLine][MINFUNC( l, LINE_LENGTH-1 )] = '\0' ;
  201.  
  202.     n = nextLine - numLines ;
  203.     if( n < 0 )
  204.     {
  205.         n = ( n + VISIBLE_LINES + 1 ) % VISIBLE_LINES ;
  206.     }
  207.  
  208.     nextLine = ( nextLine + 1 ) % VISIBLE_LINES ;
  209.     if( numLines < VISIBLE_LINES )
  210.     {
  211.         numLines = numLines + 1 ;
  212.     }
  213.  
  214.     l = 0 ;
  215.     for( k = 0 ; k < numLines ; k++ )
  216.     {
  217.         i = 0 ;
  218.         while( line[n][i] != '\0' )
  219.         {
  220.             text[l++] = line[n][i++] ;
  221.         }
  222.         text[l++] = '\n' ;
  223.  
  224.         n = ( n + 1 ) % VISIBLE_LINES ;
  225.     }
  226.     text[l++] = '\0' ;
  227.  
  228.     XmTextSetString( textArea, text ) ;
  229. }
  230.  
  231.  
  232.  
  233.